// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    @property([cc.SpriteAtlas])
    atlas: cc.SpriteAtlas = null;
    frames: cc.SpriteFrame[] = [];
    sprite: cc.Sprite = null; // Sprite组件
    index: number = 0;   // 当前显示第N张图片
    interval: number = 0.05; // 定时器的间隔
    flage: boolean = null
    timer = null
    speed:number=20
    keycod_x = {
        start: <number>null,
        end: <number>null,
        time: <number>null
    }
    setKeycod(start: number, end: number, time: number, interval: number) {
        this.keycod_x.start = start
        this.keycod_x.end = end
        this.keycod_x.time = time
        this.index = this.keycod_x.start
        this.interval = interval
    }
    onLoad() {
        this.sprite = this.getComponent(cc.Sprite);
        // 从图集中获取所有帧图片
        if (this.atlas != null)
            this.frames = this.atlas.getSpriteFrames();
        this.setKeycod(90, 95, 200, 0.1)
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    }
    init() {
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
            this.setKeycod(90, 95, 200, 0.1)
            this.schedule(this.onTimer, this.interval);
        }, this.keycod_x.time);
    }
    // 键盘按下
    onKeyDown(e: cc.Event.EventKeyboard) {
        clearTimeout(this.timer)
        if (this.flage) return
        this.flage = true
        switch (e.keyCode) {
            case cc.macro.KEY.x:
                this.setKeycod(0, 41, 200, 0.05)
                this.schedule(this.onTimer, this.interval);
                break;
            case cc.macro.KEY.f:
                this.setKeycod(159, 175, 800, 0.05)
                this.schedule(this.onTimer, this.interval);
                break;
            case cc.macro.KEY.right:
                this.setKeycod(180, 187, 200, 0.2)
                this.schedule(this.onTimer, this.interval);
                this.schedule(this.go, 0.1)
                break;
            case cc.macro.KEY.left:
                // this.setKeycod(159, 175, 800, 0.05)
                // this.schedule(this.onTimer, this.interval);
                break;
        }
    }
    go() {
        this.node.x +=this.speed
    }
    // 键盘抬起
    onKeyUp(e: cc.Event.EventKeyboard) {
        this.flage = false
        switch (e.keyCode) {
            case cc.macro.KEY.x:
                this.init()
                break;
            case cc.macro.KEY.f:
                this.init()
                break;
            case cc.macro.KEY.right:
                // this.setKeycod(159, 175, 800, 0.05)
                // this.schedule(this.onTimer, this.interval);
                this.init()
                this.unschedule(this.go)
                break;
            case cc.macro.KEY.left:
                // this.setKeycod(159, 175, 800, 0.05)
                // this.schedule(this.onTimer, this.interval);
                break;
        }
    }
    start() {
        this.schedule(this.onTimer, this.interval)
    }
    onTimer() {
        this.sprite.spriteFrame = this.frames[this.index];
        // 下一帧
        this.index++;
        if (this.index >= this.keycod_x.end)
            this.index = this.keycod_x.start;
    }
}
